﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinCalcManager;
using Infragistics.Win.CalcEngine;

namespace BYManageInfoSystem
{
    public partial class Frm_CreditCardIncome : DockContent
    {
        private DGBY.BLL.CreditCardRegisterTab m_bllCreditCardRegisterTab = new DGBY.BLL.CreditCardRegisterTab();
        private DGBY.BLL.FactoryDataTab m_bllFactoryDataTab = new DGBY.BLL.FactoryDataTab();
        private DGBY.BLL.BYDepartTab m_bllBYDepartTab = new DGBY.BLL.BYDepartTab();
        private DGBY.BLL.BYCompanyTab m_bllBYCompanyTab = new DGBY.BLL.BYCompanyTab();
        private DGBY.BLL.T_ACL_User m_bllT_ACL_User = new DGBY.BLL.T_ACL_User();
        public Frm_CreditCardIncome()
        {
            InitializeComponent();
        }

        private void Frm_CreditCardIncome_Load(object sender, EventArgs e)
        {
            InitCombox();
        }

        public static void ultraGrid1_InitializeRowFilter(Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            //ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            //foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
            //    ugc.PerformAutoResize();
            // FILTER ROW FUNCTIONALITY RELATED ULTRAGRID SETTINGS
            //e.Layout.Override.RowFilterAction = RowFilterAction.DisableFilteredOutRows;
            // ----------------------------------------------------------------------------------
            // Enable the the filter row user interface by setting the FilterUIType to FilterRow.
            e.Layout.Override.FilterUIType = FilterUIType.FilterRow;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // FilterEvaluationTrigger specifies when UltraGrid applies the filter criteria typed 
            // into a filter row. Default is OnCellValueChange which will cause the UltraGrid to
            // re-filter the data as soon as the user modifies the value of a filter cell.
            e.Layout.Override.FilterEvaluationTrigger = FilterEvaluationTrigger.OnCellValueChange;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // By default the UltraGrid selects the type of the filter operand editor based on
            // the column's DataType. For DateTime and boolean columns it uses the column's editors.
            // For other column types it uses the Combo. You can explicitly specify the operand
            // editor style by setting the FilterOperandStyle on the override or the individual
            // columns.

            e.Layout.Override.FilterOperandStyle = FilterOperandStyle.DropDownList;

            // By default UltraGrid displays user interface for selecting the filter operator. 
            // You can set the FilterOperatorLocation to hide this user interface. This
            // property is available on column as well so it can be controlled on a per column
            // basis. Default is WithOperand. This property is exposed off the column as well.
            e.Layout.Override.FilterOperatorLocation = FilterOperatorLocation.WithOperand;

            // By default the UltraGrid uses StartsWith as the filter operator. You use
            // the FilterOperatorDefaultValue property to specify a different filter operator
            // to use. This is the default or the initial filter operator value of the cells
            // in filter row. If filter operator user interface is enabled (FilterOperatorLocation
            // is not set to None) then that ui will be initialized to the value of this
            // property. The user can then change the operator as he/she chooses via the operator
            // drop down.
            e.Layout.Override.FilterOperatorDefaultValue = FilterOperatorDefaultValue.StartsWith;

            // FilterOperatorDropDownItems property can be used to control the options provided
            // to the user for selecting the filter operator. By default UltraGrid bases 
            // what operator options to provide on the column's data type. This property is
            // avaibale on the column as well.
            //e.Layout.Override.FilterOperatorDropDownItems = FilterOperatorDropDownItems.All;

            // By default UltraGrid displays a clear button in each cell of the filter row
            // as well as in the row selector of the filter row. When the user clicks this
            // button the associated filter criteria is cleared. You can use the 
            // FilterClearButtonLocation property to control if and where the filter clear
            // buttons are displayed.
            e.Layout.Override.FilterClearButtonLocation = FilterClearButtonLocation.RowAndCell;

            // Appearance of the filter row can be controlled using the FilterRowAppearance proeprty.
            e.Layout.Override.FilterRowAppearance.BackColor = Color.LightYellow;

            // You can use the FilterRowPrompt to display a prompt in the filter row. By default
            // UltraGrid does not display any prompt in the filter row.
            e.Layout.Override.FilterRowPrompt = "Click here to filter data...";

            // You can use the FilterRowPromptAppearance to change the appearance of the prompt.
            // By default the prompt is transparent and uses the same fore color as the filter row.
            // You can make it non-transparent by setting the appearance' BackColorAlpha property 
            // or by setting the BackColor to a desired value.
            e.Layout.Override.FilterRowPromptAppearance.BackColorAlpha = Alpha.Opaque;

            // By default the prompt is spread across multiple cells if it's bigger than the
            // first cell. You can confine the prompt to a particular cell by setting the
            // SpecialRowPromptField property off the band to the key of a column.
            //e.Layout.Bands[0].SpecialRowPromptField = e.Layout.Bands[0].Columns[0].Key;

            // Display a separator between the filter row other rows. SpecialRowSeparator property 
            // can be used to display separators between various 'special' rows, including for the
            // filter row. This property is a flagged enum property so it can take multiple values.
            e.Layout.Override.SpecialRowSeparator = SpecialRowSeparator.FilterRow;

            // You can control the appearance of the separator using the SpecialRowSeparatorAppearance
            // property.
            e.Layout.Override.SpecialRowSeparatorAppearance.BackColor = Color.FromArgb(233, 242, 199);
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            int nYear = this.dateTimePicker1.Value.Date.Year;
            int nMonth = this.dateTimePicker1.Value.Date.Month;

            int nYearMonth = 0;
            nYearMonth = nYear * 100 + nMonth;

            int nDeptID = Convert.ToInt32(comboBoxDep.SelectedValue);

            string strSql = "";
            if (nDeptID == 7)
            {
                strSql = " CONVERT(varchar(6), case when year(RecvMoneyDate) = 1900 then '' else RecvMoneyDate end, 112) = '" + nYearMonth + "'";
            }
            else
            {
                strSql = " BYDepartTab.BYDepartTabID = '" + nDeptID  + "'and  CONVERT(varchar(6), case when year(RecvMoneyDate) = 1900 then '' else RecvMoneyDate end, 112) = '" + nYearMonth + "'";
            }

            ShowAllRecord(strSql);
        }

        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            if (this.checkBox1.Checked == true)
                ultraGrid1_InitializeRowFilter(e);
            else
                ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);

            InitultraDropDown();
            InitColumnWidth();

            ultraGrid1.DisplayLayout.Override.WrapHeaderText = Infragistics.Win.DefaultableBoolean.True;

            foreach (UltraGridColumn column2 in ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
                column2.CellActivation = Activation.ActivateOnly;
            }
        }

        private void InitColumnWidth()
        {
            ultraGrid1.DisplayLayout.Bands[0].Columns["客户"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["业务编号"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["经营单位"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["交单金额（USD）"].Width = 90;
            ultraGrid1.DisplayLayout.Bands[0].Columns["实收款（USD）"].Width = 90;
            ultraGrid1.DisplayLayout.Bands[0].Columns["收款日"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["外汇牌价"].Width = 90;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收合计（RMB）"].Width = 90;
            ultraGrid1.DisplayLayout.Bands[0].Columns["跟单员"].Width = 45;
        }

        private void InitCombox()
        {
            DataSet ds = m_bllBYDepartTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxDep.DataSource = ds.Tables[0];
                this.comboBoxDep.DisplayMember = "部门名称";
                this.comboBoxDep.ValueMember = "BYDepartTabID";
                this.comboBoxDep.SelectedIndex = 0;
            }
        }

        private void InitultraDropDown()
        {
            DataSet dsTemp = m_bllFactoryDataTab.GetList("");

            this.ultraDropDownFac.DataSource = dsTemp.Tables[0];
            this.ultraDropDownFac.DisplayMember = "工厂简称";
            this.ultraDropDownFac.ValueMember = "FactoryDataTabID";
            this.ultraDropDownFac.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownFac.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownFac.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownFac.DisplayLayout.Bands[0].Columns["工厂简称"].Hidden = false;

            dsTemp = m_bllBYCompanyTab.GetList("");

            this.ultraDropDownComp.DataSource = dsTemp.Tables[0];
            this.ultraDropDownComp.DisplayMember = "经营单位简称";
            this.ultraDropDownComp.ValueMember = "BYCompanyTabID";
            for (int i = 0; i < this.ultraDropDownComp.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownComp.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownComp.DisplayLayout.Bands[0].Columns["经营单位简称"].Hidden = false;

            dsTemp = m_bllT_ACL_User.GetList("Name != 'System1'");

            this.ultraDropDownPerson.DataSource = dsTemp.Tables[0];
            this.ultraDropDownPerson.DisplayMember = "用户简称";
            this.ultraDropDownPerson.ValueMember = "UserPasswordTabID";
            for (int i = 0; i < this.ultraDropDownPerson.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownPerson.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownPerson.DisplayLayout.Bands[0].Columns["用户简称"].Hidden = false;
        }

        private void ShowAllRecord(string strWhere)
        {
            DataSet ds = m_bllCreditCardRegisterTab.ShowCreditCardIncome(strWhere);
            ultraGrid1.DataSource = null;
            ultraGrid1.DataSource = ds.Tables[0];

            ultraGrid1.DisplayLayout.Bands[0].Columns["CreditCardNo"].Hidden = true;
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.ultraGrid1.DisplayLayout.Bands[0].Columns["客户"].ValueList = this.ultraDropDownFac;
                this.ultraGrid1.DisplayLayout.Bands[0].Columns["经营单位"].ValueList = this.ultraDropDownComp;
                this.ultraGrid1.DisplayLayout.Bands[0].Columns["跟单员"].ValueList = this.ultraDropDownPerson;

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[i];
                    if (null == CurrentUGR)
                        continue;

                    string strCreditCardNo = ds.Tables[0].Rows[i]["CreditCardNo"].ToString();
                    if (string.IsNullOrEmpty(strCreditCardNo))
                        return;

                    DateTime dtRecvMoney = Convert.ToDateTime(ds.Tables[0].Rows[i]["收款日"].ToString());

                    DataSet dsTemp = m_bllCreditCardRegisterTab.GetList(" CreditCardNo ='" + strCreditCardNo + "' and case when year(RecvMoneyDate) = 1900 then '' else RecvMoneyDate end = '" + dtRecvMoney + "'");
                    if (dsTemp.Tables.Count > 0 && dsTemp.Tables[0].Rows.Count > 0)
                    {
                        string strSumRecv = CommonCompute.ChinaRound(Convert.ToDecimal(dsTemp.Tables[0].Rows[0]["应收合计"].ToString()) / 100, 2).ToString();
                        CurrentUGR.Cells["应收合计（RMB）"].Value = strSumRecv;
                    }
                }
            }
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }
    }
}
